/**
 * Copyright (c) 2024 Huawei Technologies Co., Ltd.
 * This file is a part of the CANN Open Software.
 * Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */

/*!
 * \file matmul_tensor_info.h
 * \brief matmul variable manager
 */

#ifndef IMPL_MATMUL_MODULES_MATMUL_TENSOR_INFO_H
#define IMPL_MATMUL_MODULES_MATMUL_TENSOR_INFO_H

#include "../matmul_module.h"

namespace matmul {
template <typename IMPL, const auto &MM_CFG, class INPUT_TYPE, typename = void>
class MatmulTensorInfo {
    using SrcT = typename INPUT_TYPE::T;
public:
    template <bool IS_INTRA_BLOCK = false>
    __aicore__ inline __gm__ SrcT* GetGlobalAddr() const
    {
        if constexpr (IS_INTRA_BLOCK) {
            return MATMUL_CONST_INTRA_BLOCK.aGlobal;
        } else {
            return MATMUL_CONST_PARAM_VAR.aGlobal_;
        }
    }

    __aicore__ inline TBuffAddr GetLocalAddr() const
    {
        return MATMUL_CONST_PARAM_VAR.leftMatrix_;
    }

    template <bool IS_INTRA_BLOCK = false>
    __aicore__ inline void SetGlobalAddr(__gm__ SrcT* srcGlobalAddr, bool isTranspose)
    {
        if constexpr (IS_INTRA_BLOCK) {
            MATMUL_INTRA_BLOCK.aGlobal = srcGlobalAddr;
            MATMUL_INTRA_BLOCK.isTransposeA = isTranspose;
        } else {
            MATMUL_PARAM_VAR.aGlobal_ = srcGlobalAddr;
            MATMUL_PARAM_VAR.isTransposeA_ = isTranspose;
        }
    }

    __aicore__ inline void SetLocalAddr(const TBuffAddr& address, bool isTranspose)
    {
        MATMUL_PARAM_VAR.leftMatrix_ = address;
        MATMUL_PARAM_VAR.isTransposeA_ = isTranspose;
    }

    __aicore__ inline uint64_t GetSelfDefineData() const
    {
        return MATMUL_CONST_PARAM_VAR.dataPtr_;
    }

    __aicore__ inline uint64_t GetUserDefineInfo() const
    {
        return MATMUL_CONST_PARAM_VAR.tilingPtr_;
    }
};

template <typename IMPL, const auto &MM_CFG, class INPUT_TYPE>
class MatmulTensorInfo<IMPL, MM_CFG, INPUT_TYPE, enable_if_t<INPUT_TYPE::TAG == InputTypeTag::B>> {
    using SrcT = typename INPUT_TYPE::T;
public:
    template <bool IS_INTRA_BLOCK = false>
    __aicore__ inline __gm__ SrcT* GetGlobalAddr() const
    {
        if constexpr (IS_INTRA_BLOCK) {
            return MATMUL_CONST_INTRA_BLOCK.bGlobal;
        } else {
            return MATMUL_CONST_PARAM_VAR.bGlobal_;
        }
    }

    __aicore__ inline TBuffAddr GetLocalAddr() const
    {
        return MATMUL_CONST_PARAM_VAR.rightMatrix_;
    }

    template <bool IS_INTRA_BLOCK = false>
    __aicore__ inline void SetGlobalAddr(__gm__ SrcT* srcGlobalAddr, bool isTranspose)
    {
        if constexpr (IS_INTRA_BLOCK) {
            MATMUL_INTRA_BLOCK.bGlobal = srcGlobalAddr;
            MATMUL_INTRA_BLOCK.isTransposeB = isTranspose;
        } else {
            MATMUL_PARAM_VAR.bGlobal_ = srcGlobalAddr;
            MATMUL_PARAM_VAR.isTransposeB_ = isTranspose;
        }
    }

    __aicore__ inline void SetLocalAddr(const TBuffAddr& address, bool isTranspose)
    {
        MATMUL_PARAM_VAR.rightMatrix_ = address;
        MATMUL_PARAM_VAR.isTransposeB_ = isTranspose;
    }

    __aicore__ inline uint64_t GetSelfDefineData() const
    {
        return MATMUL_CONST_PARAM_VAR.dataPtr_;
    }

    __aicore__ inline uint64_t GetUserDefineInfo() const
    {
        return MATMUL_CONST_PARAM_VAR.tilingPtr_;
    }
};
}      // namespace matmul
#endif // IMPL_MATMUL_MODULES_MATMUL_TENSOR_INFO_H
